package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.model.entity.ConfBd;
import com.jic.point.model.entity.ConfBdApply;
import com.jic.point.model.vo.request.ConfBdApplyRequest;
import com.jic.point.model.vo.response.ConfBdApplyResponse;
import com.jic.point.service.ConfBdApplyService;
import com.jic.point.service.ConfBdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author: admin
 * @date: 2020-11-22 20:35:05
 */
 @RestController
 @Slf4j
 @RequestMapping("/confBdApply")
 @Api(tags = " [BD部门表]控制类-PC")
public class ConfBdApplyController {
	@Autowired
	private ConfBdApplyService service;
	@Resource
    ConfBdService confBdService;


    private Object ResponseCode;


    /**
      *新增记录
      * @param request 新增[BD部门表]请求参数
      * @return ConfBdApplyResponse 新增[BD部门表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[BD部门表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfBdApplyRequest request) throws Throwable {
     		ConfBdApply confBdApply = new ConfBdApply();
    		BeanUtils.copyProperties(request, confBdApply);
    	    long flag = service.save(confBdApply);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      * @param request 新增或修改[BD部门表]请求参数
      * @return ConfBdApplyResponse 新增或修改[BD部门表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[BD部门表]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfBdApplyRequest request) throws Throwable {
         ConfBdApply confBdApply = new ConfBdApply();
         BeanUtils.copyProperties(request, confBdApply);
         long flag =   service.saveOrUpdate(confBdApply);
         return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[BD部门表]请求参数
      * @return ConfBdApplyResponse 删除[BD部门表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[BD部门表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody ConfBdApplyRequest request) throws Throwable {
      		ConfBdApply confBdApply = new ConfBdApply();
	     	BeanUtils.copyProperties(request, confBdApply);
	         long flag = service.deleteByPrimaryKeyLogically(confBdApply);
      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[BD部门表]请求参数
      * @return ConfBdApplyResponse 修改[BD部门表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[BD部门表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody ConfBdApplyRequest request) throws Throwable {
      		ConfBdApply confBdApply = new ConfBdApply();
	     	BeanUtils.copyProperties(request, confBdApply);
	         long flag = service.updateByPrimaryKeySelective(confBdApply);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[BD部门表]详情请求参数
      * @return ConfBdApplyResponse 查询[BD部门表]详情响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[BD部门表]详情")
      @ResponseBody
     public RestResult<ConfBdApplyResponse> getByPrimaryKey(@Validated  @RequestBody ConfBdApplyRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	ConfBdApply confBdApply = new ConfBdApply();
	     	BeanUtils.copyProperties(request, confBdApply);
	     	ConfBdApply result = service.getByPrimaryKey(confBdApply);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
//		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     ConfBdApplyResponse bo = new ConfBdApplyResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new ConfBdApplyResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<ConfBdApply>> listPage (@RequestBody ConfBdApplyRequest request) throws Throwable {
      ConfBdApply param = new ConfBdApply();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *CONF_BD_APPLY 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [BD部门表] ")
      @ResponseBody
      public RestResult<List<ConfBdApplyResponse>>  listConfBdApplyAllRecord(@RequestBody ConfBdApplyRequest request) throws Throwable {
      		ConfBdApply param = new ConfBdApply();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<ConfBdApply> list = service.listAllRecord(param);
			List<ConfBdApplyResponse> responseList = list.stream().map(temp -> {
			ConfBdApplyResponse bo = new ConfBdApplyResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }
    @PostMapping("/auditAubmit")
    @ApiOperation(value = "审核提交 [第三方机构主表] ")
    @ResponseBody
    public RestResult<Long> auditAubmit(@RequestBody ConfBdApplyRequest request){
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        ConfBdApply param = new ConfBdApply();
        BeanUtils.copyProperties(request, param);
        ConfBdApply result = service.getByPrimaryKey(param);
        //赋值
        result.setAuditSts(request.getAuditSts());
        result.setAuditReason(request.getAuditReason());
        result.setAuditorId(request.getAdminOperate().getOperator());
        //判断审核结果（1成功and2失败）
        if(result.getAuditSts() == 1){
            //审核成功---------判断当前动作（0增加---1编辑----2删除）
            if(result.getCurrentAction() == 0){
                //申请表逻辑删除-----当前操作人id
                service.deleteByPrimaryKeyLogically(result);
                //插入到主表中
                ConfBd confBd = new ConfBd();
                BeanUtils.copyProperties(result, confBd);
                confBd.setId(null);
                confBd.setDeleteFlag(0);
                confBd.setAuditSts(1);
                long flag = confBdService.insertSelective(confBd);
                return  RestResult.success(flag);

            }else if(result.getCurrentAction() == 1){
                //申请表逻辑删除-----当前操作人id
                service.deleteByPrimaryKeyLogically(result);
                //通过主表的code字段，修改delete_flag为0
                ConfBd confBd = new ConfBd();
                BeanUtils.copyProperties(result, confBd);
                confBd.setDeleteFlag(0);
                confBd.setAuditSts(1);
                long flag = confBdService.saveOrUpdateApply(confBd);
                return  RestResult.success(flag);
            }else{
                //申请表逻辑删除-----当前操作人id
                result.setAuditSts(1);
                long flag=  service.deleteByPrimaryKeyLogically(result);
                return  RestResult.success(flag);
            }
        }else {
            if(result.getCurrentAction() == 0 ){
                //审核失败------新增------申请表逻辑删除
                result.setAuditSts(2);
                long flag = service.deleteByPrimaryKeyLogically(result);
                return  RestResult.success(flag);
            }else if(result.getCurrentAction() == 1 || result.getCurrentAction() == 2){
                //审核失败------编辑-----删除
                service.deleteByPrimaryKeyLogically(result);
                //通过主表的code字段，修改delete_flag为0
                ConfBd confBd = new ConfBd();
                BeanUtils.copyProperties(result, confBd);
                confBd.setDeleteFlag(0);
                confBd.setAuditSts(2);
                long flag = confBdService.saveOrUpdateApply(confBd);
                return  RestResult.success(flag);
            }
        }
          return  null;
    }
 }

	
